package com.zhn.shortlink.admin.toolkit;

import java.util.Random;

/**
 * 分组id随机生成器
 */
public final class RandomGenerator {

    private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz";
    private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase();
    private static final String NUMBERS = "0123456789";
    private static final int LENGTH = 6;
    private static final int TOTAL_CHARACTERS = CHAR_LOWER.length() + CHAR_UPPER.length() + NUMBERS.length();
    private static final Random RANDOM = new Random();

    /**
     * 短链接分组id生成
     * 随机生成6位字符串包含字母和数字
     * @return 随机结果
     */
    public static String generateRandomString() {
        StringBuilder builder = new StringBuilder(LENGTH);

        for (int i = 0; i < LENGTH; i++) {
            int index = RANDOM.nextInt(TOTAL_CHARACTERS);

            if (index < CHAR_LOWER.length()) {
                builder.append(CHAR_LOWER.charAt(index));
            } else if (index < CHAR_LOWER.length() + CHAR_UPPER.length()) {
                builder.append(CHAR_UPPER.charAt(index - CHAR_LOWER.length()));
            } else {
                builder.append(NUMBERS.charAt(index - CHAR_LOWER.length() - CHAR_UPPER.length()));
            }
        }

        return builder.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateRandomString());
    }
}